$( document ).ready(function() {
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("[name='appointPatientId']").val("");
	$("[name='appointPatientFirstname']").val("");
	$("[name='appointPatientLastname']").val("");
	$("[name='appointDocId']").val("");
	$("[name='appointDocFirstname']").val("");
	$("[name='appointDocLastname']").val("");
	$("[name='appointmentId']").val("");
	$("[name='confirmedPatientId']").val("");
	$("[name='confirmedDocId']").val("");
});

function getPatientSearchResult() {
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("#appointPatientSearchResult").empty();
	$.getJSON("staff/appointPatientSearch.html?appointPatientId="+$("[name='appointPatientId']").val()+"&appointPatientFirstname="+$("[name='appointPatientFirstname']").val()+"&appointPatientLastname="+$("[name='appointPatientLastname']").val(), function(data) {
		if(!jQuery.isEmptyObject(data.appointPatientSearchResult)){
			$("#appointPatientSearchResult").data("searchResult", data);
			printTableBody(data);
			$("#appointPatientSearchResult").append("<br /><button id=\"confirmPatient\" onclick=\"confirmPatient()\" class=\"button\">confirm</button>&nbsp;");
		}else{
			$("#appointPatientSearchResult").append("<strong>No result.</strong>");
		}
		
	});
}

function clearPtable(){
	$("#appointPatientSearchResult").empty();
}

function printTableBody(data) {
	
	$("#appointPatientSearchResult")
			.append(
					"<table id=\"list\" class=\"tablecss\">" +
					"<thead>" +
					"<tr>" +
					"<th></th>"+
					"<th>Patient id</th>" +
					"<th>First name</th>" +
					"<th>Last name</th>" +
					"<th>Gender</th>" +
					"<th>Date of Birth</th>" +
					"</tr>" +
					"</thead>" +
					"<tbody id=\"tableBody\">" +
					"</tbody>" +
					"</table>");
	var tr;
	$(data).each(
			function(index, value) {
				$(value.appointPatientSearchResult).each(function (i,v){
					tr = $("<tr>");
					tr.append("<td><input type=\"radio\" name=\"patientRadio\" value=\""+v.userId+"\"></td>");
					tr.append("<td>" + v.userId + "</td>");
					tr.append("<td>" + v.firstname + "</td>");
					tr.append("<td>" + v.lastname + "</td>");
					var gen=v.gender;
					if(gen=='m'){
						tr.append("<td>male</td>");
					}else if(gen=='f'){
						tr.append("<td>female</td>");
					}else{
						tr.append("<td></td>");
					}
					tr.append("<td>" + v.dateOfBirth.substring(0,10) +"</td>");
					tr.append("</tr>");
					$("#tableBody").append(tr);
				});
				
			});
	$("#list").tablesorter({
		widgets : [ 'zebra' ],
		headers : {
			0 : {
				sorter : false
			}
		}
	});
}

function confirmPatient(){
	var id=$("[name=\"patientRadio\"]:checked").val();
	$("[name=\"confirmedPatientId\"]").val(id);
	$("#appointPatientSearchResult").empty();
}

function getDocSearchResult(){
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("#appointDocSearchResult").empty();
	$.getJSON("staff/appointDocSearch.html?appointDocId="+$("[name='appointDocId']").val()+"&appointDocFirstname="+$("[name='appointDocFirstname']").val()+"&appointDocLastname="+$("[name='appointDocLastname']").val(), function(data) {
		if(!jQuery.isEmptyObject(data.appointDocSearchResult)){
			$("#appointDocSearchResult").data("docSearchResult", data);
			printDocTableBody(data);
			$("#appointDocSearchResult").append("<br /><button id=\"confirmDoc\" onclick=\"confirmDoc()\" class=\"button\">confirm</button>&nbsp;");
		}else{
			$("#appointDocSearchResult").append("<strong>No result.</strong>");
		}
		
	});
}

function clearDtable(){
	$("#appointDocSearchResult").empty();
}

function printDocTableBody(data){
	$("#appointDocSearchResult")
	.append(
			"<table id=\"docList\" class=\"tablecss\">" +
			"<thead>" +
			"<tr>" +
			"<th></th>"+
			"<th>Doctor id</th>" +
			"<th>First name</th>" +
			"<th>Last name</th>" +
			"<th>Gender</th>" +
			"<th>Office phone number</th>" +
			"<th>Office room</th>" +
			"<th>Department</th>" +
			"</tr>" +
			"</thead>" +
			"<tbody id=\"docTableBody\">" +
			"</tbody>" +
			"</table>");
var tr;
$(data).each(
	function(index, value) {
		$(value.appointDocSearchResult).each(function (i,v){
			tr = $("<tr>");
			tr.append("<td><input type=\"radio\" name=\"docRadio\" value=\""+v.userId+"\"></td>");
			tr.append("<td>" + v.userId + "</td>");
			tr.append("<td>" + v.firstname + "</td>");
			tr.append("<td>" + v.lastname + "</td>");
			var gen=v.gender;
			if(gen=='m'){
				tr.append("<td>male</td>");
			}else if(gen=='f'){
				tr.append("<td>female</td>");
			}else{
				tr.append("<td></td>");
			}
			tr.append("<td>" +v.officeInfo.officePhone+"</td>");
			tr.append("<td>" +v.officeInfo.officeDept.officeRoom+"</td>");
			tr.append("<td>" +v.officeInfo.officeDept.deptInfo.deptName+"</td>");
			tr.append("</tr>");
			$("#docTableBody").append(tr);
		});
		
	});
$("#docList").tablesorter({
	widgets : [ 'zebra' ],
	headers : {
		0 : {
			sorter : false
		}
	}
});
}

function confirmDoc(){
	var id=$("[name=\"docRadio\"]:checked").val();
	$("[name=\"confirmedDocId\"]").val(id);
	$("#appointDocSearchResult").empty();
}

function getAppointmentSearchResult(){
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("#appointmentSearchResult").empty();
	$.getJSON("staff/searchAppointment.html?appointmentId="+$("[name='appointmentId']").val()+"&confirmedPatientId="+$("[name='confirmedPatientId']").val()+"&confirmedDocId="+$("[name='confirmedDocId']").val(), function(data) {
		if(!jQuery.isEmptyObject(data.docSchedules)){
			$("#appointmentSearchResult").data("appointmentSearchResult", data);
			$("#appointmentSearchResult").data("paramAppointmentId",$("[name='appointmentId']").val());
			$("#appointmentSearchResult").data("paramPatientId",$("[name='confirmedPatientId']").val());
			$("#appointmentSearchResult").data("paramDocId",$("[name='confirmedDocId']").val());
			printAppointTableBody(data);
		}else{
			$("#appointmentSearchResult").append("<strong>No result.</strong>");
		}
		
	});
}

function printAppointTableBody(data){
	$("#appointmentSearchResult")
	.append(
			"<table id=\"appointList\" class=\"tablecss\">" +
			"<thead>" +
			"<tr>" +
			"<th>Appointment id</th>" +
			"<th>Patient id</th>" +
			"<th>Patient name</th>" +
			"<th>Doctor id</th>" +
			"<th>Doctor name</th>" +
			"<th>Operate staff id</th>" +
			"<th>Operate staff name</th>" +
			"<th>Appointment time</th>" +
			"<th></th>"+
			"</tr>" +
			"</thead>" +
			"<tbody id=\"appointTableBody\">" +
			"</tbody>" +
			"</table>");
var tr;
$(data).each(
	function(index, value) {
		$(value.docSchedules).each(function (i,v){
			tr = $("<tr>");
			tr.append("<td>" + v.appointmentId + "</td>");
			tr.append("<td>" + v.patientId + "</td>");
			tr.append("<td>" + v.patientName + "</td>");
			tr.append("<td>" + v.doctorId + "</td>");
			tr.append("<td>" + v.doctorName + "</td>");
			tr.append("<td>" + v.staffId + "</td>");
			tr.append("<td>" + v.staffName + "</td>");
			tr.append("<td>"+convertDateTime(v.appointmentTime)+"</td>");
			tr.append("<td><button id=\"updateAppoint\" onclick=\"updateAppoint("+v.appointmentId+")\" class=\"button\">update</button>&nbsp;"+
			"<button id=\"deleteAppoint\" onclick=\"deleteAppoint("+v.appointmentId+")\" class=\"button\">delete</button></td>");
			tr.append("</tr>");
			$("#appointTableBody").append(tr);
		});
		
	});
$("#appointList").tablesorter({
	widgets : [ 'zebra' ],
	headers : {
		8 : {
			sorter : false
		}
	}
});
}



function updateAppoint(appointmentId){
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	var appointmentDiv=$("#appointmentSearchDiv").html();
	$("#appointmentSearchDiv").data("appointmentDiv", appointmentDiv);
	$("#appointmentSearchDiv").empty();
	var appointment=null;
	var appointList=$("#appointmentSearchResult").data("appointmentSearchResult");
	$(appointList).each(function(index, value){
		$(value.docSchedules).each(function (i,v){
			if(v.appointmentId==appointmentId){
				appointment=v;
			}
		});
	});
	var appointmentTime=new Date(appointment.appointmentTime);
	$("#appointmentSearchResult").empty();
	$("#appointmentSearchResult").append("<form class=\"regular\" action=\"javascript:confirmUpdate("+appointment.appointmentId+");\">" +
			"<h1>update appointment</h1>" +
					"<label for=\"updatePatientId\">Patient id: </label>" +
			"<input type=\"text\" id=\"updatePatientId\" name=\"confirmedPatientId\" value=\""+appointment.patientId+"\">" +
					"<label for=\"updateDoctorId\">Doctor id: </label>" +
			"<input type=\"text\" id=\"updateDoctorId\" name=\"confirmedDocId\" value=\""+appointment.doctorId+"\">" +
					"<label for=\"updateDate\">Date: </label>" +
			"<input id=\"updateDate\" name=\"date\" type=\"text\">" +
					"<label for=\"updateTime\">Time: </label>" +
			"<input id=\"updateTime\" name=\"time\" type=\"text\">" +
			"<button id=\"confirmUpdate\" type=\"submit\" class=\"button\">confirm</button>&nbsp;" +
			"<button id=\"cancelUpd\" type=\"button\" onclick=\"cancelUpdate()\" class=\"button\">cancel</button>" +
			"</form>");
	$("#updateDate").datepicker("setDate",appointmentTime);
	$("#updateDate").datepicker("update");
	$("#updateTime").timepicker({'timeFormat': 'H:i'});
	$("#updateTime").timepicker("setTime",appointmentTime);
	
	
}

function cancelUpdate(){
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	var paramAppointmentId=$("#appointmentSearchResult").data("paramAppointmentId");
	var paramPatientId=$("#appointmentSearchResult").data("paramPatientId");
	var paramDocId=$("#appointmentSearchResult").data("paramDocId");
	$("#appointmentSearchResult").empty();
	var appointmentDiv=$("#appointmentSearchDiv").data("appointmentDiv");
	$("#appointmentSearchDiv").html(appointmentDiv);
	$("[name='appointmentId']").val(paramAppointmentId);
	$("[name='confirmedPatientId']").val(paramPatientId);
	$("[name='confirmedDocId']").val(paramDocId);
	$.getJSON("staff/searchAppointment.html?appointmentId="+$("[name='appointmentId']").val()+"&confirmedPatientId="+$("[name='confirmedPatientId']").val()+"&confirmedDocId="+$("[name='confirmedDocId']").val(), function(data) {
		if(!jQuery.isEmptyObject(data.docSchedules)){
			$("#appointmentSearchResult").data("appointmentSearchResult", data);
			$("#appointmentSearchResult").data("paramAppointmentId",$("[name='appointmentId']").val());
			$("#appointmentSearchResult").data("paramPatientId",$("[name='confirmedPatientId']").val());
			$("#appointmentSearchResult").data("paramDocId",$("[name='confirmedDocId']").val());
			printAppointTableBody(data);
		}else{
			$("#appointmentSearchResult").append("<strong>No result.</strong>");
		}
		
	});
}

function confirmUpdate(appointmentId){
	var paramAppointmentId=$("#appointmentSearchResult").data("paramAppointmentId");
	var paramPatientId=$("#appointmentSearchResult").data("paramPatientId");
	var paramDocId=$("#appointmentSearchResult").data("paramDocId");
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	
	$.getJSON("staff/updateAppointment.html?relistAppointmentId="+paramAppointmentId+"&relistPatientId="+paramPatientId+"&relistDocId="+paramDocId+"&updateAppointId="+appointmentId+"&confirmedPatientId="+$("#updatePatientId").val()+"&confirmedDocId="+$("#updateDoctorId").val()+"&date="+$("#updateDate").val()+"&time="+$("#updateTime").val(),function(data){
		if(true==data.flag){
			$("#appointmentSearchResult").empty();
			var appointmentDiv=$("#appointmentSearchDiv").data("appointmentDiv");
			$("#appointmentSearchDiv").html(appointmentDiv);
			$("[name='appointmentId']").val(paramAppointmentId);
			$("[name='confirmedPatientId']").val(paramPatientId);
			$("[name='confirmedDocId']").val(paramDocId);
			if(!jQuery.isEmptyObject(data.docSchedules)){
				$("#appointmentSearchResult").data("appointmentSearchResult", data);
				$("#appointmentSearchResult").data("paramAppointmentId",$("[name='appointmentId']").val());
				$("#appointmentSearchResult").data("paramPatientId",$("[name='confirmedPatientId']").val());
				$("#appointmentSearchResult").data("paramDocId",$("[name='confirmedDocId']").val());
				printAppointTableBody(data);
			}else{
				$("#appointmentSearchResult").append("<strong>No result.</strong>");
			}
			$("#messageDiv").removeClass("success");
			$("#messageDiv").removeClass("error");
			$("#messageDiv").addClass("success");
			$("#messageDiv").html("<strong>"+data.message+"</strong>");
			$("#messageDiv").show();
			
		}else if(data.flag==false){
			$("#messageDiv").removeClass("success");
			$("#messageDiv").removeClass("error");
			$("#messageDiv").addClass("error");
			$("#messageDiv").html("<strong>"+data.message+"</strong>");
			$("#messageDiv").show();
		}
		
	});
	
}

function deleteAppoint(appointmentId){
	
	var paramAppointmentId=$("#appointmentSearchResult").data("paramAppointmentId");
	var paramPatientId=$("#appointmentSearchResult").data("paramPatientId");
	var paramDocId=$("#appointmentSearchResult").data("paramDocId");
	$("#appointmentSearchResult").empty();
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$.getJSON("staff/deleteAppointment.html?deleteAppointId="+appointmentId+"&appointmentId="+paramAppointmentId+"&confirmedPatientId="+paramPatientId+"&confirmedDocId="+paramDocId, function(data) {
		if(true==data.flag){
			if(!jQuery.isEmptyObject(data.docSchedules)){
				$("#appointmentSearchResult").data("appointmentSearchResult", data);
				$("#appointmentSearchResult").data("paramAppointmentId",$("[name='appointmentId']").val());
				$("#appointmentSearchResult").data("paramPatientId",$("[name='confirmedPatientId']").val());
				$("#appointmentSearchResult").data("paramDocId",$("[name='confirmedDocId']").val());
				printAppointTableBody(data);
			}else{
				$("#appointmentSearchResult").append("<strong>No result.</strong>");
			}
			$("#messageDiv").removeClass("success");
			$("#messageDiv").removeClass("error");
			$("#messageDiv").addClass("success");
			$("#messageDiv").html("<strong>"+data.message+"</strong>");
			$("#messageDiv").show();
		}else if(data.flag==false){
			$("#messageDiv").removeClass("success");
			$("#messageDiv").removeClass("error");
			$("#messageDiv").addClass("error");
			$("#messageDiv").html("<strong>"+data.message+"</strong>");
			$("#messageDiv").show();
		}
		
	});
	
}

function convertDateTime(milliSec){
	var dateTime=new Date(milliSec);
	var format=new String("");
	format=dateTime.getFullYear()+"-";
	if((dateTime.getMonth()+1)<10){
		format=format+"0"+(dateTime.getMonth()+1)+"-";
	}else{
		format=format+(dateTime.getMonth()+1)+"-";
	}
	if(dateTime.getDate()<10){
		format=format+"0"+dateTime.getDate()+" ";
	}else{
		format=format+dateTime.getDate()+" ";
	}
	if(dateTime.getHours()<10){
		format=format+"0"+dateTime.getHours()+":";
	}else{
		format=format+dateTime.getHours()+":";
	}
	if(dateTime.getMinutes()<10){
		format=format+"0"+dateTime.getMinutes();
	}else{
		format=format+dateTime.getMinutes();
	}
	return format;
}
